﻿Imports System.Drawing

Public Class Led
    Private M_OnColor As Color = Color.SkyBlue
    Private M_OffColor As Color = Color.DarkBlue
    Private M_State As LedState = LedState.Led_Off
    Private M_Flashing As Boolean = False
    Private M_Border As Integer = 2
    Private M_Led As Boolean = False
    Private M_X As Integer = 1
    Private M_Y As Integer = 1
    Private M_FixedSize As Boolean = True
    Public Enum LedState
        Led_Off = 0
        Led_On = 1
        Led_Flash = 2
        Led_Pulse = 3
    End Enum
    Public Property On_Color() As Color
        Get
            Return M_OnColor
        End Get
        Set(ByVal value As Color)
            M_OnColor = value
            Me.Refresh()
        End Set
    End Property
    Public Property Off_Color() As Color
        Get
            Return M_OffColor
        End Get
        Set(ByVal value As Color)
            M_OffColor = value
            Me.Refresh()
        End Set
    End Property
    Public Property State() As LedState
        Get
            Return M_State
        End Get
        Set(ByVal value As LedState)
            M_State = value
            If M_State = LedState.Led_Off Then
                M_Led = False
                M_Flashing = False
                Timer1.Enabled = False
            End If
            If M_State = LedState.Led_On Then
                M_Led = True
                M_Flashing = False
                Timer1.Enabled = False
            End If
            If M_State = LedState.Led_Flash Then
                M_Led = False
                M_Flashing = True
                Timer1.Enabled = True
            End If
            If M_State = LedState.Led_Pulse Then
                M_Led = True
                M_Flashing = False
                Timer1.Interval = 250
                Timer1.Enabled = True
            End If
            Me.Refresh()
        End Set
    End Property

    Private Sub Led_BackColorChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.BackColorChanged
        Me.Refresh()
    End Sub

    Private Sub Led_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        'e.Graphics.Clear(Me.BackColor)
        Dim B As Integer = M_Border
        Dim x As Integer = B - M_X
        Dim y As Integer = B - M_Y
        Dim w As Integer = Me.Width - (B * 2)
        Dim h As Integer = Me.Height - (B * 2)

        If M_Led = True Then
            Dim BR As New SolidBrush(M_OnColor)
            e.Graphics.FillEllipse(BR, x, y, w, h)
            e.Graphics.DrawEllipse(Pens.Black, x, y, w, h)
        Else
            Dim BR As New SolidBrush(M_OffColor)
            e.Graphics.FillEllipse(BR, x, y, w, h)
            e.Graphics.DrawEllipse(Pens.Black, x, y, w, h)
        End If
    End Sub

    Private Sub Led_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If M_FixedSize Then
            Me.Width = 20
            Me.Height = 20
        End If
    End Sub
    Public Property FixedSize() As Boolean
        Get
            Return M_FixedSize
        End Get
        Set(ByVal value As Boolean)
            M_FixedSize = value
        End Set
    End Property
    Public Property LedSize() As Integer
        Get
            Return M_Border
        End Get
        Set(ByVal value As Integer)
            M_Border = value
            Me.Refresh()
        End Set
    End Property
    Public Property X_Offset() As Integer
        Get
            Return M_X
        End Get
        Set(ByVal value As Integer)
            M_X = value
            Me.Refresh()
        End Set
    End Property
    Public Property Y_Offset() As Integer
        Get
            Return M_Y
        End Get
        Set(ByVal value As Integer)
            M_Y = value
            Me.Refresh()
        End Set
    End Property

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If M_Flashing = True Then
            If M_Led = True Then
                M_Led = False
                Timer1.Interval = 500
                Timer1.Enabled = True
            Else
                Timer1.Interval = 250
                Timer1.Enabled = True
                M_Led = True
            End If
        Else
            Timer1.Enabled = False
        End If
        If M_State = LedState.Led_Pulse Then M_State = LedState.Led_Off
        Me.Refresh()
    End Sub
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(Windows.Forms.ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(Windows.Forms.ControlStyles.SupportsTransparentBackColor, True)
        Me.UpdateStyles()
        Me.BackColor = Color.Transparent
    End Sub
End Class
